package com.letou.ls.view;

import java.util.ArrayList;
import java.util.List;

import com.letou.ls.R;
import com.letou.ls.uitl.UiSizeHelper;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class PriorityDlg extends Dialog {
    private Context context;
    private GridView dlg_grid = null;
    private List<String> mList = new ArrayList<String>();
    private int position;
    protected LayoutInflater mInflater;
    private MyAdapter mMyAdapter;
    
    private SelectItemListener mSelectItemListener;

    public PriorityDlg(Context context) {
        super(context, R.style.rotation_load_dialog);
        this.context = context;
    }

    public PriorityDlg(Context context, int type) {
        super(context, R.style.rotation_load_dialog);
        this.context = context;
    }

    public PriorityDlg(Context context, int theme, String str) {
        super(context, R.style.rotation_load_dialog);
        this.context = context;
    }

    

    public void setList(List mList) {
        this.mList = mList;
       // Log.e("lb", "setList:" + mList.size());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.color_dialog);
        getWindow().getAttributes().gravity = Gravity.CENTER;
        getWindow().getAttributes().height = UiSizeHelper.getScreenHeight() - 100;
        getWindow().getAttributes().width = UiSizeHelper.getScreenWidth() - 200;
        dlg_grid = (GridView) findViewById(R.id.gridview);
        mInflater = LayoutInflater.from(context);
        mMyAdapter = new MyAdapter();
        dlg_grid.setAdapter(mMyAdapter);
        
        dlg_grid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String colorValues = mList.get(position);
				if(mSelectItemListener!=null){
					mSelectItemListener.onItemClick(colorValues);	
				}
				dismiss();
			}
		});

    }
    
    public void showDailog( SelectItemListener selectItemListener){
    	  this.mSelectItemListener = selectItemListener;
    	  this.show();
    }
    
    private class MyAdapter extends BaseAdapter {
        
        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.color_dialog_item, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.tv_text = (TextView) convertView.findViewById(R.id.tv_text);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            try {
                // viewHolder.tv_text.setText(mList.get(position));
                viewHolder.tv_text.setBackgroundColor(Color.parseColor(mList.get(position)));
            } catch (Exception e) {
                Log.e("kk", "Exception-->" + mList.get(position));
            }
            
            return convertView;
        }
        
    }
    class ViewHolder {
        public TextView tv_text;
    }
    
    public interface SelectItemListener{
    	public void onItemClick(String colorValue);
    }
}
